import 'package:flutter/material.dart';

import '../contants/constant.dart';
import '../core/http/client.dart';
import '../model/db_utils.dart';
import '../test/tanslate_baidu.dart';
import '../test/test.dart';

class ApiUtils {
  /**
   * 爱词霸翻译 本地编译
   */
  static void getTranslateByIciBaLocal(
      String translate, Function netBack) async {
    try {
      dynamic mapResult = await _translateItem(translate);
      mapResult["zh"] = translate;
      var response = mapResult;
      debugPrint("getTranslateIciBaByOkHttp:${response.toString()}");
      if (response.toString().isNotEmpty) {
        netBack(response);
      } else {
        netBack("");
      }
    } catch (e) {
      print(e);
    }
  }

  static Future<dynamic> _translateItem(translate) async {
    final ici_ba = Iciba();
    var mapResult = Map<String, dynamic>();
    var listConfig = await DbUtils().loadTranslateConfig();
    //注意 forEach 里面不要有耗时操作 否者 会提前返回数据
    for (int i = 0; i < listConfig.length; i++) {
      var element = listConfig[i];
      if (element[Constant.isNeedTranslate]) {
        var result = await ici_ba.icibaApi(translate,
            fromLanguage: "zh", toLanguage: element[Constant.apiName]);
        mapResult[element[Constant.apiName]] = result;
      }
    }
    return Future.value(mapResult);
  }

  /**
   * 百度翻译 本地编译
   */
  static void getTranslateByBaiDuLocal(
      String translate, Function netBack) async {
    final ici_ba = Iciba();
    try {
      var en = await baiduTranslate(query: translate, to: "en");
      var ko = await baiduTranslate(query: translate, to: "kor");
      var ja = await baiduTranslate(query: translate, to: "jp");
      // var tr = await baiduTranslate(query: translate, to: "tr");//
      var tr = await ici_ba.icibaApi(translate,
          fromLanguage: "zh", toLanguage: "tr"); //百度不支持 暂时用爱词霸代替
      var cs = await baiduTranslate(query: translate, to: "cs");
      var ar = await baiduTranslate(query: translate, to: "ara");
      var it = await baiduTranslate(query: translate, to: "it");
      var de = await baiduTranslate(query: translate, to: "de");
      var es = await baiduTranslate(query: translate, to: "spa");
      var ro = await baiduTranslate(query: translate, to: "rom");
      var ru = await baiduTranslate(query: translate, to: "ru");
      var nl = await baiduTranslate(query: translate, to: "nl");
      var uk = await baiduTranslate(query: translate, to: "ukr");
      var cht = await baiduTranslate(query: translate, to: "cht");
      var pl = await baiduTranslate(query: translate, to: "pl");
      var fr = await baiduTranslate(query: translate, to: "fra");
      // var mn = await baiduTranslate(query: translate, to: "mn");
      var mn = await ici_ba.icibaApi(translate,
          fromLanguage: "zh", toLanguage: "mn"); //百度不支持 暂时用爱词霸代替
      var th = await baiduTranslate(query: translate, to: "th");
      var bg = await baiduTranslate(query: translate, to: "bul"); //保加利亚语
      var hu = await baiduTranslate(query: translate, to: "hu"); //匈牙利
      var sk = await baiduTranslate(query: translate, to: "sk"); //斯洛伐克
      var kz = await baiduTranslate(query: translate, to: "ka"); //哈萨克语
      print("th:${th}");
      var map = Map<String, dynamic>();
      map["zh"] = translate;
      map["en"] = en[0]["dst"];
      map["ko"] = ko[0]["dst"];
      map["ja"] = ja[0]["dst"];
      map["tr"] = tr;
      map["cs"] = cs[0]["dst"];
      map["ar"] = ar[0]["dst"];
      map["it"] = it[0]["dst"];
      map["de"] = de[0]["dst"];
      map["es"] = es[0]["dst"];
      map["ro"] = ro[0]["dst"];
      map["ru"] = ru[0]["dst"];
      map["uk"] = uk[0]["dst"];
      map["zhTW"] = cht[0]["dst"];
      map["pl"] = pl[0]["dst"];
      map["fr"] = fr[0]["dst"];
      map["mn"] = mn;
      map["nl"] = nl[0]["dst"];
      map["th"] = th[0]["dst"];
      map["bg"] = bg[0]["dst"];
      map["hu"] = hu[0]["dst"];
      map["sk"] = sk[0]["dst"];
      map["kz"] = kz[0]["dst"];
      var response = map;
      debugPrint("getTranslateIciBaByOkHttp:${response.toString()}");
      if (response.toString().isNotEmpty) {
        netBack(response);
      } else {
        netBack("");
      }
    } catch (e) {
      print(e);
    }
  }

  /**
   * 爱词霸翻译
   */
  static void getTranslateByIciBa(String translate, Function netBack) async {
    String decoded = Uri.encodeComponent(translate); // 将 UTF-8 编码的字节序列解码为字符串
    var response = await get(apiArea: "/translate_iciba?name=${decoded}");
    debugPrint("getTranslateIciBaByOkHttp:${response.toString()}");
    if (response != null && response.toString().isNotEmpty) {
      netBack(response);
    } else {
      netBack("");
    }
  }

  /**
   * 金山翻译
   */
  static void getTranslateByJinShan(String translate, Function netBack) async {
    String decoded = Uri.encodeComponent(translate); // 将 UTF-8 编码的字节序列解码为字符串
    var response = await get(apiArea: "/translate_jin_shan?name=${decoded}");
    debugPrint("getTranslateByJinShan:${response.toString()}");
    if (response != null && response.toString().isNotEmpty) {
      netBack(response);
    } else {
      netBack("");
    }
  }

  /**
   * 百度翻译
   */
  static void getTranslateByBaidu(String translate, Function netBack) async {
    String decoded = Uri.encodeComponent(translate); // 将 UTF-8 编码的字节序列解码为字符串
    var response = await get(apiArea: "/translate_baidu?name=${decoded}");
    debugPrint("getTranslateByJinShan:${response.toString()}");
    if (response != null && response.toString().isNotEmpty) {
      netBack(response);
    } else {
      netBack("");
    }
  }

  /**
   * 谷歌翻译
   */
  static void getTranslateByGoogle(String translate, Function netBack) async {
    String decoded = Uri.encodeComponent(translate); // 将 UTF-8 编码的字节序列解码为字符串
    var response = await get(apiArea: "/translate_google?name=${decoded}");
    debugPrint("getTranslateByJinShan:${response.toString()}");
    if (response != null && response.toString().isNotEmpty) {
      netBack(response);
    } else {
      netBack("");
    }
  }
}
